//
//  SCSQLiteManager.swift
//  GZWeibo007
//
//  Created by newuser on 16/1/3.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit


let T_Status = "T_Status"

class SCSQLiteManager: NSObject {

    //单列
    static let sharedManager = SCSQLiteManager()
    
    //数据库
    var dbQueue: FMDatabaseQueue
    
    private override init() {
        //拼接路径
        let documentPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        let dbPath = (documentPath as NSString).stringByAppendingPathComponent("status.db")
        //赋值
        dbQueue = FMDatabaseQueue(path: dbPath)
        
        print("dbPath: \(dbPath)")
        
        super.init()
        createTable(T_Status)
    }
    
    //createTime TEXT DEFAULT (datetime('now', 'localtime')), sql获取的系统当前时间
    //创建表
    func createTable(tName: String) {
        
        let sql = "CREATE TABLE IF NOT EXISTS \(tName) ( \n" +
            "statusId INTEGER NOT NULL, \n" +
            "status TEXT, \n" +
            "userId INTEGER, \n" +
            "createTime TEXT DEFAULT (datetime('now', 'localtime')), \n" +
        "PRIMARY KEY(statusId))"
    
        dbQueue.inDatabase { (db) -> Void in
            if db.executeUpdate(sql) {
            print("执行sql: \(sql)")
                print("创建表成功")
            } else {
               print("创建表失败")
            }
        }
        
    }
}
